package admin.controllers;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import admin.data.EmailStatisticData;

import models.EmailStatisticModel;
import models.OrderModel;
import search.OrderSearchData;

@SuppressWarnings("serial")
public class StatisticServlet extends HttpServlet {

	private static final Logger log = Logger.getLogger(StatisticServlet.class
			.getName());

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		process(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		process(request, response);
	}

	protected void process(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		try {

			String action = request.getParameter("action");
			if (action != null && action.equals("email_marketing")) {
				String strFromDate = request.getParameter("fromDate");
				String strToDate = request.getParameter("toDate");
				Date fromDate;
				Date toDate;
				DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
				try {
					fromDate = formatter.parse(strFromDate);
					request.setAttribute("fromDate", strFromDate);
				} catch (Exception e) {
					fromDate = null;
				}
				try {
					toDate = formatter.parse(strToDate);
					request.setAttribute("toDate", strToDate);
				} catch (Exception e) {
					toDate = null;
				}

				int orderCount = 0;
				int displayMoney = 0;
				int realMoney = 0;

				OrderSearchData orderSearch = new OrderSearchData();
				int page = 1;
				while (true) {
					orderSearch.setLimit(488);
					orderSearch.setPage(page);
					orderSearch.searchByEmailMarketing(fromDate, toDate);
					for (OrderModel order : orderSearch.getListResult()) {
						orderCount++;
						displayMoney += order.getTotalPrice();
						if (order.getStatus() == 4) {
							realMoney += order.getTotalPrice();
						}
					}
					if (orderSearch.getListResult() != null
							&& orderSearch.getListResult().size() < 488) {
						break;
					} else {
						page++;
					}
				}

				int clickCount = 0;
				int totalEmail = 0;
				int totalDeliveryEmail = 0;
				EmailStatisticData statisticData = new EmailStatisticData();
				List<EmailStatisticModel> listStatistic = statisticData
						.getStatisticByDate(fromDate, toDate); 
				for (EmailStatisticModel emailStatisticModel : listStatistic) {
					clickCount += emailStatisticModel.getClickCount();
					totalEmail += emailStatisticModel.getEmailCount();
					totalDeliveryEmail += emailStatisticModel
							.getDeliveryEmailCount();
				}

				request.setAttribute("fromDate", strFromDate);
				request.setAttribute("toDate", strToDate);
				request.setAttribute("totalDeliveryEmail", totalDeliveryEmail);
				request.setAttribute("totalEmail", totalEmail);
				request.setAttribute("clickCount", clickCount);
				request.setAttribute("orderCount", orderCount);
				request.setAttribute("displayMoney", displayMoney);
				request.setAttribute("realMoney", realMoney);
				request.getRequestDispatcher(
						"/admin/pages/statistic_email_marketing.jsp").forward(
						request, response);
			}

		} catch (ServletException e) {
			System.out.println("Error when try to get statistic email !");
			log.warning("Error when try to get statistic email  !");
			log.warning(e.toString());
			e.printStackTrace();
		}
	}
}
